//
//  AFMineViewController.m
//  AFChat
//
//  Created by Void on 2018/6/21.
//  Copyright © 2018年 afei. All rights reserved.
//

#import "AFMineViewController.h"

#import "AFMineHeaderCell.h"

#import "AFUserHelper.h"
#import "AFMenuItem.h"
#import "AFUIHelper.h"

static NSString * const ID = @"MineCell";

@interface AFMineViewController ()

@property (nonatomic, strong) AFUser *user;

@end

@implementation AFMineViewController


- (id)init {
    if (self = [super init]) {
       initTabBarItem(self.tabBarItem, LOCSTR(@"我"), @"tabbar_me", @"tabbar_meHL");
        [self.tabBarItem setBadgeValue:@""];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitle:LOCSTR(@"我")];
    
    [self.tableView registerClass:[AFMineHeaderCell class] forCellReuseIdentifier:ID];
    self.tableView.tableFooterView = [UIView new];
    
    // 加载cell 数据
    [self loadData];
}

#pragma mark - TableName delegate/ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.data ? self.data.count + 1 : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    AFMenuItemGrounp *group = [self.data objectAtIndex:section - 1];
    return group.itemsCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AFMineHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        cell.user = self.user;
        cell.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    return [super tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section - 1]];
}

#pragma mark - <UITableViewDelegate>
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 90.0f;
    }
    return [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section - 1]];
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return [super tableView:tableView heightForFooterInSection:0];
    }
    return [super tableView:tableView heightForFooterInSection:section - 1];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - load Data
- (void)loadData {
    self.data = [AFUIHelper getMineVCItems];
    self.user = [AFUserHelper sharedHelper].user;
    [self.tableView reloadData];
}

@end
